package cn.xutongxue.gateway.config;

import cn.dev33.satoken.reactor.filter.SaReactorFilter;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * [Sa-Token 权限认证] 配置类 
 * @author click33
 */
@Configuration
public class SaTokenConfigure {
    // 注册 Sa-Token全局过滤器
    @Bean
    public SaReactorFilter getSaReactorFilter() {
        return new SaReactorFilter()
            // 拦截地址
            .addInclude("/**")
            // 开放地址
            .addExclude("/favicon.ico")
            .setAuth(obj -> {
                SaRouter.match("/**")
                        // 放行knife4j的资源
                        .notMatch("/doc.html", "/swagger-resources/**", "/webjars/**", "/v2/api-docs/**", "/swagger-ui.html")
                        .notMatch("/v3/api-docs/**")
                        .notMatch("/user/login", "/user/register")
                        .check(r-> StpUtil.checkLogin());
            })
            .setError(e -> {
                return SaResult.error(e.getMessage());
            })
            ;
    }
}
